package com.qf.gh.web.servlet;

import cn.dsna.util.images.ValidateCode;
import com.qf.gh.domain.Address;
import com.qf.gh.domain.User;
import com.qf.gh.service.AddressService;
import com.qf.gh.service.UserService;
import com.qf.gh.service.impl.AddressServiceImpl;
import com.qf.gh.service.impl.UserServiceImpl;
import com.qf.gh.utils.Base64Utils;
import com.qf.gh.utils.StrUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/userservlet")
public class UserServlet extends BaseServlet{
    UserService userService = new UserServiceImpl();
    AddressService addressService=new AddressServiceImpl();

    public String defaultAddress(HttpServletRequest request, HttpServletResponse response){
        User user = (User) request.getSession().getAttribute("user");
        String id = request.getParameter("id");
        addressService.setDefaultAddress(Integer.parseInt(id), user.getId());
        return "redirect:/userservlet?method=getAddress";
    }

    public String deleteAddress(HttpServletRequest request, HttpServletResponse response) throws IOException {
        User user = (User) request.getSession().getAttribute("user");
        String id = request.getParameter("id");
        addressService.deleteById(Integer.parseInt(id));
        return "redirect:/userservlet?method=getAddress";
    }

    public String getAddress(HttpServletRequest request, HttpServletResponse response) throws IOException {
        User user = (User) request.getSession().getAttribute("user");
        List<Address> addressList = addressService.findByUid(user.getId());
        request.setAttribute("addList",addressList);
        return "forward:/self_info.jsp";
    }

    public String updateAddress(HttpServletRequest request, HttpServletResponse response) throws IOException {
        User user = (User) request.getSession().getAttribute("user");
        String name = request.getParameter("name");
        String phone = request.getParameter("phone");
        String detail = request.getParameter("detail");
        String id = request.getParameter("id");
        String level = request.getParameter("level");
        if (StrUtils.isEmpty(name) || StrUtils.isEmpty(phone) || StrUtils.isEmpty(detail)||StrUtils.isEmpty(id)||StrUtils.isEmpty(level)) {
            System.out.println("数据有误，请重新添加");
        }
        Address address = new Address();
        address.setDetail(detail);
        address.setLevel(Integer.parseInt(level));
        address.setName(name);
        address.setPhone(phone);
        address.setUid(user.getId());
        address.setId(Integer.parseInt(id));
        addressService.update(address);
        return "redirect:/userservlet?method=getAddress";
    }

    public String addAddress(HttpServletRequest request, HttpServletResponse response) throws IOException {
        User user= (User) request.getSession().getAttribute("user");
        String name = request.getParameter("name");
        String phone = request.getParameter("phone");
        String detail = request.getParameter("detail");
        if(StrUtils.isEmpty(name)||StrUtils.isEmpty(phone)||StrUtils.isEmpty(detail)){
            System.out.println("数据有误，请重新添加");
            return "redirect:/userservlet?method=getAddress";
        }
        Address address=new Address();
        address.setDetail(detail);address.setLevel(0);address.setName(name);address.setPhone(phone);
        address.setUid(user.getId());
        addressService.addAddress(address);
        return "redirect:/userservlet?method=getAddress";
    }
    public String login(HttpServletRequest request, HttpServletResponse response)throws Exception{
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        if(StrUtils.isEmpty(username)||StrUtils.isEmpty(password)){
            return "redirect:/login.jsp";
        }
        User user=userService.login(username,password);
        if(user==null){
            return "redirect:/login.jsp";
        }else{
            request.getSession().setAttribute("user",user);
            String auto = request.getParameter("auto");
            if("on".equalsIgnoreCase(auto)){
                String userinfo=username+"#"+password;
                Cookie cokkie=new Cookie("userinfo", Base64Utils.encode(userinfo));
                cokkie.setPath("/");
                cokkie.setMaxAge(14*24*60*60);
                response.addCookie(cokkie);
            }
            return "redirect:/index.jsp";
        }
    }
    public String logOut(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.getSession().removeAttribute("user");
        request.getSession().invalidate();
        Cookie cookie = new Cookie("userinfo", Base64Utils.encode(""));
        cookie.setMaxAge(1);
        cookie.setPath("/");
        response.addCookie(cookie);
        return "redirect:/login.jsp";
    }
    public String register(HttpServletRequest request, HttpServletResponse response) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String email = request.getParameter("email");
        String gender = request.getParameter("gender");
        if(StrUtils.isEmpty(username) || StrUtils.isEmpty(email) || StrUtils.isEmpty(password)) {
            return "redirect:/register.jsp";
        }
        boolean flag = userService.checkUsername(username);
        if(flag) {
            return "redirect:/register.jsp";
        }
        boolean isSuccess = userService.register(username, password, email, gender);
        if(isSuccess){
            return "redirect:/login.jsp";
        } else {
            return "redirect:/register.jsp";
        }
    }
    public void checkUserName(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String username = request.getParameter("username");
        if(StrUtils.isEmpty(username)){
            response.getWriter().print("0");
        } else {
            boolean flag = userService.checkUsername(username);
            if(flag) {
                response.getWriter().print("1");
            }else {
                response.getWriter().print("2");
            }
        }
    }
    public void codee(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ValidateCode validateCode=new ValidateCode(100,35,2,15);
        request.getSession().setAttribute("code",validateCode.getCode());
        validateCode.write(response.getOutputStream());

    }
    public void checkCode1(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String code = request.getParameter("code");
        if(StrUtils.isEmpty(code)){
            response.getWriter().print("1");
            return;
        }
        String code1 = (String) request.getSession().getAttribute("code");
        if(code.equalsIgnoreCase(code1)){
            response.getWriter().print("0");
        }else{
            response.getWriter().print("1");
        }
    }
}
